import React from 'react';
import moment from 'moment';
import fx from '../../investmentAnalysis/utils/fx';
import dicts from '../../investmentAnalysis/dicts';

const colorMoney = (text) => {
  const n = Number.parseFloat(text);
  const t = fx.formatMoney(text);
  const color = n < 0 ? 'green' : 'red';
  return (<span style={{ color }}>{t}</span>);
};

const columns = [
  {
    title: '股票代码',
    dataIndex: 'symbol',
    sorter: true,
  },
  {
    title: '类型',
    dataIndex: 'tradeType',
    sorter: true,
    render: text => dicts.tradeType[text],
  },
  {
    title: '买入价格',
    dataIndex: 'buyPrice',
    render: text => fx.formatNumber(text, 2),
    sorter: true,
  },
  {
    title: '卖出价格',
    dataIndex: 'sellPrice',
    render: text => fx.formatNumber(text, 2),
    sorter: true,
  },
  {
    title: '交易数量',
    dataIndex: 'quantity',
    sorter: true,
  },
  {
    title: '费用',
    dataIndex: 'fee',
    render: text => fx.formatNumber(text, 2),
    sorter: true,
  },
  {
    title: '建仓时间',
    dataIndex: 'openTime',
    render: text => moment(text).format('YYYY-MM-DD HH:mm:ss'),
    sorter: true,
  },
  {
    title: '平仓时间',
    dataIndex: 'closeTime',
    render: text => moment(text).format('YYYY-MM-DD HH:mm:ss'),
    sorter: true,
  },
  {
    title: '盈亏',
    dataIndex: 'yield',
    render: (text, row) => {
      const { tradeType, buyPrice, sellPrice, quantity, fee } = row;
      return colorMoney((sellPrice - buyPrice) * quantity - fee);
    },
  },
];

export default columns;
